/**
 * Created by ${PRODUCT_NAME}.
 * User: skylerparr
 * Date: 12/26/10
 * Time: 11:52 AM
 * To change this template use File | Settings | File Templates.
 */
package com.thoughtorigin.loader {
import flash.display.Loader;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.LoaderContext;

public class LoaderItem extends AbstractLoader {

    private var _loader: Loader;

    public function LoaderItem(url: String) {
        super(url);
    }

    override public function start():void {
        if(isLoading) {
            return;
        }

        isLoading = true;
        _loader = createLoader();
        addListeners();
        _loader.load(urlRequest, loaderContext);
    }

    private function onLoadComplete(e: Event): void {
        cleanupAndDispatch(e);
    }

    override public function get content():* {
        return _loader.content;
    }

    override public function get loader():* {
        return _loader;
    }

    private function onError(e: Event): void {
        cleanupAndDispatch(e);
    }

    private function onInit(e: Event): void {
        dispatchEvent(e);
    }

    protected function cleanupAndDispatch(e: Event): void {
        dispatchEvent(e);
        removeListeners();
    }

    override public function stop():void {
        removeListeners();
    }

    override public function pause():void {

    }

    override public function resume():void {

    }

    private function onProgressHandler(e: Event): void {
        dispatchEvent(e);
    }

    protected function addListeners(): void {
        contentDispatcher.addEventListener(Event.COMPLETE, onLoadComplete);
        contentDispatcher.addEventListener(Event.INIT, onInit);
        contentDispatcher.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
        contentDispatcher.addEventListener(IOErrorEvent.NETWORK_ERROR, onError);
        contentDispatcher.addEventListener(IOErrorEvent.IO_ERROR, onError);
        contentDispatcher.addEventListener(IOErrorEvent.VERIFY_ERROR, onError);
        contentDispatcher.addEventListener(IOErrorEvent.DISK_ERROR, onError);
        contentDispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
    }

    protected function removeListeners(): void {
        contentDispatcher.removeEventListener(Event.COMPLETE, onLoadComplete);
        contentDispatcher.removeEventListener(Event.INIT, onInit);
        contentDispatcher.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler);
        contentDispatcher.removeEventListener(IOErrorEvent.NETWORK_ERROR, onError);
        contentDispatcher.removeEventListener(IOErrorEvent.IO_ERROR, onError);
        contentDispatcher.removeEventListener(IOErrorEvent.DISK_ERROR, onError);
        contentDispatcher.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
    }

    protected function createLoader(): * {
        return new Loader();
    }

    override public function get contentDispatcher():IEventDispatcher {
        return _loader.contentLoaderInfo;
    }
}
}
